Don't read ~/.cargo/config in tests
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 25 Mar 2017 11:17:51 +0000 (14:17 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 25 Mar 2017 11:58:20 +0000 (14:58 +0300)
Closes #3863

src/cargo/core/workspace.rs
src/cargo/util/config.rs
src/cargo/util/paths.rs
tests/cargotest/lib.rs

index 1b420e8a4abf63fe5268da5956bf31c034a5eb84..5015448a569cef6494b82825e2ef72fa836ab076 100644 (file)
@@ -266,8 +266,7 @@ impl<'cfg> Workspace<'cfg> {
             }
         }
 
-        let mut cur = manifest_path.parent().and_then(|p| p.parent());
-        while let Some(path) = cur {
+        for path in paths::ancestors(manifest_path).skip(2) {
             let manifest = path.join("Cargo.toml");
             debug!("find_root - trying {}", manifest.display());
             if manifest.exists() {
@@ -286,7 +285,6 @@ impl<'cfg> Workspace<'cfg> {
                     WorkspaceConfig::Member { .. } => {}
                 }
             }
-            cur = path.parent();
         }
 
         Ok(None)
index a715306637c94d436f8c860ecbb5f0292e5e8ad2..c0527a760691114021de6908043dc690c4ef2a1f 100644 (file)
@@ -17,6 +17,7 @@ use core::shell::{Verbosity, ColorConfig};
 use core::MultiShell;
 use util::{CargoResult, CargoError, ChainError, Rustc, internal, human};
 use util::{Filesystem, LazyCell};
+use util::paths;
 
 use util::toml as cargo_toml;
 
@@ -698,10 +699,9 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
 fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
     where F: FnMut(File, &Path) -> CargoResult<()>
 {
-    let mut current = pwd;
     let mut stash: HashSet<PathBuf> = HashSet::new();
 
-    loop {
+    for current in paths::ancestors(pwd) {
         let possible = current.join(".cargo").join("config");
         if fs::metadata(&possible).is_ok() {
             let file = File::open(&possible)?;
@@ -710,11 +710,6 @@ fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
 
             stash.insert(possible);
         }
-
-        match current.parent() {
-            Some(p) => current = p,
-            None => break,
-        }
     }
 
     // Once we're done, also be sure to walk the home directory even if it's not
index d47598a2ece148aab39d562967182408cd50954b..48273ae9263d5a12176faa06c04b2a19079cc028 100644 (file)
@@ -142,3 +142,42 @@ pub fn bytes2path(bytes: &[u8]) -> CargoResult<PathBuf> {
         Err(..) => Err(human("invalid non-unicode path")),
     }
 }
+
+pub fn ancestors(path: &Path) -> PathAncestors {
+    PathAncestors::new(path)
+}
+
+pub struct PathAncestors<'a> {
+    current: Option<&'a Path>,
+    stop_at: Option<PathBuf>
+}
+
+impl<'a> PathAncestors<'a> {
+    fn new(path: &Path) -> PathAncestors {
+        PathAncestors {
+            current: Some(path),
+            //HACK: avoid reading `~/.cargo/config` when testing Cargo itself.
+            stop_at: env::var("__CARGO_TEST_ROOT").ok().map(PathBuf::from),
+        }
+    }
+}
+
+impl<'a> Iterator for PathAncestors<'a> {
+    type Item = &'a Path;
+
+    fn next(&mut self) -> Option<&'a Path> {
+        if let Some(path) = self.current {
+            self.current = path.parent();
+
+            if let Some(ref stop_at) = self.stop_at {
+                if path == stop_at {
+                    self.current = None;
+                }
+            }
+
+            Some(path)
+        } else {
+            None
+        }
+    }
+}
index d6a9be7a768a30ad99e451102882fabae6051599..1b5899b7f8c5910b275bcc3eacea17bc5cd0ac8c 100644 (file)
@@ -50,6 +50,7 @@ fn _process(t: &OsStr) -> cargo::util::ProcessBuilder {
      .env_remove("CARGO_HOME")
      .env("HOME", support::paths::home())
      .env("CARGO_HOME", support::paths::home().join(".cargo"))
+     .env("__CARGO_TEST_ROOT", support::paths::root())
      .env_remove("RUSTC")
      .env_remove("RUSTFLAGS")
      .env_remove("CARGO_INCREMENTAL")